package vista;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import xml.XMLClass;
import negocio.beans.central.ItemListaPrecio;
import negocio.beans.central.ListaPrecio;
import negocio.beans.central.Proveedor;
import controlador.CasaCentral;
import daohibernate.ItemListaPrecioDAO;
import daohibernate.ListaPrecioDAO;


public class ListaDePrecios extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	private JTable tblListasPrecio;

	private JLabel lblXML;
	private JTextField txtXmlName;
	private JButton btnCargar;
	
	private String razonSocial;
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ListaDePrecios inst = new ListaDePrecios("");
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ListaDePrecios(String razonSocial) {
		super();
		initGUI();
		CentrarJFrame();
		this.razonSocial = razonSocial;
		LlenarTablaListaPrecios();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(202,203,251));
			getContentPane().setLayout(null);
			this.setResizable(false);
			this.setTitle("Listas de Precio");
			{
				TableModel tblListasPrecioModel = 
					new DefaultTableModel(
							new String[][] { { "Nombre"}},
							new String[] { "clmNombre" });
				tblListasPrecio = new JTable();
				getContentPane().add(tblListasPrecio);
				tblListasPrecio.setModel(tblListasPrecioModel);
				tblListasPrecio.setBounds(19, 20, 283, 234);
			}
			{
				lblXML = new JLabel();
				getContentPane().add(lblXML);
				lblXML.setText("XML:");
				lblXML.setBounds(7, 270, 79, 14);
			}
			{
				txtXmlName = new JTextField();
				getContentPane().add(txtXmlName);
				txtXmlName.setBounds(141, 270, 138, 21);
			}
			{
				btnCargar = new JButton();
				getContentPane().add(btnCargar);
				btnCargar.setText("Cargar");
				btnCargar.setBounds(290, 270, 100, 21);
				btnCargar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						
						XMLClass xml = new XMLClass();
						ListaPrecio lp = xml.flistaPrecio(txtXmlName.getText().toString());
						
						ListaPrecioDAO.getInstancia().altaListaPrecio(lp);
						
						Set<ItemListaPrecio> items = lp.getItems();
						
						lp = ListaPrecioDAO.getInstancia().traerListaPrecio(lp.getTipo(), lp.getProveedor().getCuit());
						
						for(ItemListaPrecio ilp : items)
						{
							ilp.setListaPrecio(lp);
							ItemListaPrecioDAO.getInstancia().altaItemListaPrecio(ilp);
						}
					
						LlenarTablaListaPrecios();
					}
				});
			}
			
			pack();
			this.setSize(442, 400);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void CentrarJFrame(){
		
	    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	    
	    
	    int w = this.getSize().width;
	    int h = this.getSize().height;
	    int x = (dim.width-w)/2;
	    int y = (dim.height-h)/2;
	    
	    
	    this.setLocation(x, y);
	}
	
	private void LlenarTablaListaPrecios(){
		List<ListaPrecio> listas = CasaCentral.getInstancia().obtenerListasPrecio(razonSocial);
				
		DefaultTableModel temp = (DefaultTableModel) tblListasPrecio.getModel();
		
		for(ListaPrecio l : listas){
									
			Object nuevo[]= {l.getId_listaPrecio()};
			temp.addRow(nuevo);
		}
	}
	
	public void setRazonSocial(String razonSocial){
		this.razonSocial = razonSocial;
	}

}
